package com.aia.prm.web.ria.app.ui.gpinsu;

import java.util.List;

import org.apache.log4j.Logger;

import com.aia.aia2.domain.Gpinsu;
import com.aia.aia2.persistence.dao.GpinsuDAO;
import com.aia.prm.BeanFactory;
import com.aia.prm.web.ria.app.PrmApplication;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.VerticalSplitPanel;

@SuppressWarnings("serial")
public class GpinsuView extends VerticalLayout implements Property.ValueChangeListener {
	private static final Logger logger = Logger.getLogger(GpinsuView.class);
	private PrmApplication app = null;
	VerticalSplitPanel vert = null;
	HorizontalSplitPanel horiz = null;
	VerticalLayout rightLayout = null;
	GpinsuList GpinsuList = null;
	Form form = null;
	GpinsuContainer GpinsuContainer = null;
	GpinsuForm GpinsuForm = null;
	private GpinsuDAO gpinsuDAO = null;
	List<Gpinsu> list = null;

	VerticalLayout topLayout = null;
	Button newButton = null;

	public GpinsuView(PrmApplication aapp) {
		app = aapp;
		setGpinsuDAO((GpinsuDAO) BeanFactory.get().getBean("gpinsuDAO"));
		buildMainLayout();
		load();
	}

	void load() {
		list = getGpinsuDAO().selectByExample(null);
		if (GpinsuContainer == null) {
			try {
				GpinsuContainer = new GpinsuContainer();
			} catch (Throwable e) {
				logger.error(e);
				throw new RuntimeException(e);
			}
		}

		GpinsuContainer.removeAllItems();
		GpinsuContainer.addAll(list);

		GpinsuList.setContainerDataSource(GpinsuContainer);

		GpinsuList.setVisibleColumns(GpinsuContainer.NATURAL_COL_ORDER);
		GpinsuList.setColumnHeaders(GpinsuContainer.COL_HEADERS_SHOW);

		GpinsuForm.setItemDataSource(null);
	}

	private void buildMainLayout() {
		setSizeFull();
		vert = new VerticalSplitPanel();
		vert.setSizeFull();
		vert.setSplitPosition(60, Sizeable.UNITS_PIXELS);
		addComponent(vert);

		// add a label to the upper area
		topLayout = new VerticalLayout();
		topLayout.setSpacing(true);
		newButton = new Button("添加记录", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				GpinsuForm.addObject();
			}
		});

		newButton.setIcon(new ThemeResource("icons/add.png"));
		HorizontalLayout topLayoutSub = new HorizontalLayout();

		topLayoutSub.setSizeUndefined();
		topLayoutSub.setMargin(true);
		topLayoutSub.setSpacing(true);

		topLayoutSub.addComponent(newButton);
		topLayout.addComponent(topLayoutSub);
		vert.setFirstComponent(topLayout);

		horiz = new HorizontalSplitPanel();
		horiz.setSplitPosition(66); // percent
		vert.setSecondComponent(horiz);

		// left component:
		GpinsuList = new GpinsuList(app, this);
		horiz.setFirstComponent(GpinsuList);

		rightLayout = new VerticalLayout();
		rightLayout.setSizeUndefined();
		rightLayout.setWidth("100%");
		rightLayout.setMargin(true);
		GpinsuForm = new GpinsuForm(this);

		rightLayout.addComponent(GpinsuForm);
		// right component:
		horiz.setSecondComponent(rightLayout);

	}

	public void valueChange(ValueChangeEvent event) {
		Item item = GpinsuList.getItem(GpinsuList.getValue());
		GpinsuForm.setItemDataSource(item);
	}

	public GpinsuDAO getGpinsuDAO() {
		return gpinsuDAO;
	}

	public void setGpinsuDAO(GpinsuDAO gpinsuDAO) {
		this.gpinsuDAO = gpinsuDAO;
	}

}
